// pages/uploadimg/index.js
Page({
    data: {
        files: [],
        canvasWidth: 300,
        canvasHeight: 200,
        uploadings: [],
        uploadProgress: []
    },
    chooseImage: function (e) {
        var that = this;
        wx.chooseImage({
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths

                that.setData({
                    files: that.data.files.concat(tempFilePaths),
                });

                var uploadProgress = that.data.uploadProgress;
                var uploadings = that.data.uploadings;
                for (f in tempFilePaths) {
                    console.info("遍历上传的文件")
                    uploadProgress = uploadProgress.concat([0])
                    uploadings = uploadings.concat("weui-uploader__file_status")
                }

                that.setData({
                    uploadProgress: uploadProgress,
                    uploadings: uploadings
                });


                //开始压缩 客户端压缩还有不少问题，暂时先进行服务器端的压缩吧
                // that.getCanvasImg(0, 0, tempFilePaths);

                that.uploadCanvasImg(0, tempFilePaths)
                //开始上传
            }
        })
    },
    previewImage: function (e) {
        //图片预览
        wx.previewImage({
            current: e.currentTarget.id, // 当前显示图片的http链接
            urls: this.data.files // 需要预览的图片http链接列表
        })
    },
    //上传图片
    uploadCanvasImg: function (index, canvasImgs) {
        var that = this;
        if (index < canvasImgs.length) {
            console.info("上传图片：%s", canvasImgs[index])

            var uploadProgress = that.data.uploadProgress;
            var uploadIndex = index + that.data.files.length - canvasImgs.length;
            var p = 0;
            var pp = 10;
            var invervalId = setInterval(function () {
                if (p == 90) {
                    pp = 1;
                }
                if (p == 99) {
                    clearInterval(invervalId);
                }
                uploadProgress[uploadIndex] = p;
                that.setData({
                    uploadProgress: uploadProgress
                })
                p += pp;
            }, 1000)


            var tempImg = canvasImgs[index];
            const uploadTask = wx.uploadFile({
                url: "https://tddtest.zcjb.com.cn/common/sysFile/uploadftp",//文件服务器的地址
                // url: "http://localhost:8910/common/sysFile/uploadftp",//文件服务器的地址
                filePath: tempImg,
                header: {
                    "sessionId": "9105a3c5-f0e8-4375-9372-8480ccd3b221"
                },
                formData: {
                    path: "/wxmp/"
                },
                name: 'file',
                success: function (res) {
                    console.info("上传成功")

                    var uploadProgress = that.data.uploadProgress;
                    uploadProgress[uploadIndex] = 100;
                    that.setData({
                        uploadProgress: uploadProgress
                    })

                    console.info(res.data)
                    that.uploadCanvasImg(index + 1, canvasImgs)
                }, complete: function () {
                    clearInterval(invervalId);
                    var uploadings = that.data.uploadings;
                    uploadings[uploadIndex] = "";
                    that.setData({
                        uploadings: uploadings
                    })
                }
            })

       
        }
    }
})